<?php

/*
  Project: CI Forum
  Type: PHP and database
  Desc: Contains all functions related to categories.
  Author: Yashpal, Munish, Chander 
 
  Function name : 
  save_forum     -> Insert forum into database
  save_category  -> Insert category into database
  save_thread    -> Insert thread into database
  save_reply     -> Insert reply into database


*/

class Adminforummodel extends Model {

    function Adminforummodel() {
    // Call the Model constructor
        parent::Model();
        $this->load->helper('date');
    }











	/*chan*/


    function delete_forum($post_id,$page_id) {
        $page_id=self::RefinePagNav($page_id);
        $this->db->where('forum_id', $post_id);
        $query=$this->db->delete('board_forums');
        redirect('admin/manage_forums/'.$page_id, 'refresh');
    }


    function edit_forum($forum_id,$page_id) {

		/*	dohash - by default is sha algo 		
		    default user role*/
    //switch case to find proper value
        $update = array(
            'forum_title' => $this->input->post('forum_title'),
            'cat_title' => $this->input->post('cat_title'),
            'forum_desc' => $this->input->post('forum_desc'),
        );

        $this->db->where(array('forum_id'=>$forum_id));
        $check=$this->db->update('board_forums', $update);


        if($check) {
        //User successfully registered and message generated.
            $info['info']="<p>Forum successfully updated.<p>";
        }
        else {
        //User Registration failed due to unknown error.
        //Most common error is wrong database settings.
            $info['info']="Unexpected Error...Updation Failed.";

        }

        $info['title']="Information Page";
        $info['header']="Information";
        $info['url']="/admin/manage_forums/".$page_id;
        $info['time']="2";

        $this->load->view('info',$info);



    }

    function manage_forums($pageid) {
        $pageid=self::RefinePagNav($pageid);

        $this->db->from('board_forums');
        $cnt=$this->db->count_all_results();

        $config['base_url'] = site_url().'/admin/manage_forums/';
        $config['total_rows'] =$cnt;
        $config['per_page'] = '10';
        $config['num_links'] = '3';
        $config['uri_segment'] = '3';


        $this->pagination->initialize($config);
        $this->db->order_by("forum_title", "desc");
        $query = $this->db->get('board_forums',$config['per_page'],$this->uri->segment(3));

        $out= "<table width=\"100%\" border=\"0\">";
        $out.="<caption>Forums List</caption>
			<thead>
				<tr>
					<th scope=\"col\">Forum Title</th>
					<th scope=\"col\">Category Title</th>
					<th scope=\"col\">Edit</th>
					<th scope=\"col\">Delete</th>
				</tr>
			</thead>	
			<tfoot>
				<tr>
					<th scope=\"row\">Total</th>

					<td colspan=\"3\">$cnt Posts</td>
				</tr>
			</tfoot>
			<tbody>";

        foreach ($query->result() as $row) {
            $out.=
                "<tr>
			    <td>".$row->forum_title."</td>
			    <td>".$row->cat_title."</td>
			    <td>".anchor('admin/edit_forum/'.$row->forum_id,'Edit')."</td>
			    <td>".anchor('admin/delete_forum/'.$row->forum_id.'/'.$pageid,'Delete')."</td>
			  </tr>";


        }
        $out.= "</tbody></table>";
        $out.= $this->pagination->create_links();
        return $out;

    }

	/************************************************************/

    function RefinePagNav($page_id) {
        if($page_id==1)
            $page_id=0;

        return $page_id;
    }



}?>